<?php

class Public_Applet_TagsSuccessView extends BlogPublicBaseView
{
	public function executeHtml(AgaviRequestDataHolder $rd)
	{
		$this->setupHtml($rd);
		$model = $this->context->getModel('Posts', 'Public'); /** @var PostsModel */ 
		
		$tags = array();
		$total = 0.0; // total number of all tags occurences
        $max_cnt = 0;
        $min_cnt = 1000000;

		foreach ($model->findTags() as $pair) {
		    list($tag, $cnt) = $pair;
		    
		    $total += $cnt;
		    $tags[] = array($tag, $cnt);
            if ($cnt > $max_cnt) {
                $max_cnt = $cnt;
            }
            if ($cnt < $min_cnt) {
                $min_cnt = $cnt;
            }
		}
        //echo '<pre>';
        //var_dump($max_cnt, $min_cnt);
        //exit();

        $max_cnt -= $min_cnt;
        // $max_cnt -> 9
        // $min_cnt -> 0
		
		foreach ($tags as &$pair) {
            $cnt = $pair[1];
            $cnt -= $min_cnt;
		    $weight = (int)(($cnt / $max_cnt) * 10);
		    $pair[1] = $weight;
		}
		$this->setAttribute('tags', $tags);
	}
}

?>
